package com.hetwen.search.demo.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hetwen.search.demo.ISendMsg;
import com.hetwen.search.demo.R;
import com.hetwen.search.demo.push.PushService;
import com.hetwen.search.demo.view.BannerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ISendMsg sender;
    private LinearLayout wrapper;
    private EditText etInput;
    private ServiceConnection serviceConn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        BannerView bannerView = (BannerView) findViewById(R.id.content_main_banner);
        List<String> list = new ArrayList<>();
        list.add("http://139.129.13.2/media/upload/banner/banner1.png");
        list.add("http://139.129.13.2/media/upload/banner/banner2.png");
        list.add("http://139.129.13.2/media/upload/banner/banner3.png");
        list.add("http://139.129.13.2/media/upload/banner/banner4.png");
        bannerView.refreshBanner(list);

        wrapper = (LinearLayout) findViewById(R.id.content_main_wrapper);
        etInput = (EditText) findViewById(R.id.content_main_input);
        Button btnSend = (Button) findViewById(R.id.content_main_send);
        btnSend.setOnClickListener(this);

        Intent intent = new Intent(this, PushService.class);
        startService(intent);
        serviceConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                sender = ISendMsg.Stub.asInterface(iBinder);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };
        bindService(intent, serviceConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConn);
        Intent intent = new Intent(this, PushService.class);
        stopService(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_search) {
            startActivity(new Intent(this, SearchActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.content_main_send:
                sendMsg();
                break;
            default:
                break;
        }
    }

    private void sendMsg() {
        String msg = etInput.getText().toString();
        if (msg != null && msg != "") {
            if (sender != null) {
                try {
                    sender.send(msg);
                    afterSend(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void afterSend(String msg) {
        etInput.setText("");
        TextView tvMsg = new TextView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(0,10,10,20);
        params.gravity = Gravity.RIGHT;
        tvMsg.setLayoutParams(params);
        tvMsg.setGravity(Gravity.CENTER);
        tvMsg.setText(msg);
        wrapper.addView(tvMsg);
    }
}
